﻿Class CheckCode
    ''' <summary>
    ''' 百世物流
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function BaiShiTong3(ByVal mCode As String) As String
        Dim i, la As Integer
        Dim SumV, Re As Integer
        Dim tmp As String
        la = Len(mCode)
        For i = 1 To la
            tmp = Mid(mCode, i, 1)
            If i Mod 2 <> 0 Then
                SumV = SumV + Val(tmp) * 3
            Else
                SumV = SumV + Val(tmp) * 1
            End If
        Next i
        Re = SumV Mod 10
        If 10 - Re >= 10 Then
            BaiShiTong3 = "0"
        Else
            BaiShiTong3 = Trim(Str(10 - Re))
        End If
    End Function
    ''' <summary>
    ''' 信丰2位
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function XinFeng1245(ByVal mCode As String) As String '??1?
        Dim STR2, STR1, R As String
        STR1 = Right(mCode, 1)
        STR2 = Right(mCode, 2)
        STR2 = Left(STR2, 1)
        If STR1 = "0" Then
            R = "9"
        Else
            R = Str(System.Math.Abs(Val(STR1) - 1))
        End If
        XinFeng1245 = Trim(R)
    End Function
    ''' <summary>
    ''' 信丰3位
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function XinFeng3(ByVal mCode As String) As String '??3?
        Dim x1, STR2, STR1, R, x2 As String
        STR1 = Right(mCode, 1)
        STR2 = Right(mCode, 2)
        STR2 = Left(STR2, 1)
        If STR1 = "0" Then
            R = "9"
        Else
            R = Str(System.Math.Abs(Val(STR1) - 1))
        End If
        x1 = Right(Str(Val(STR1) * 6 + 3), 1)
        x2 = Right(Str(Val(STR2) * 8 + 5), 1)
        XinFeng3 = Trim(R & x1 & x2)
    End Function
    ''' <summary>
    ''' 顺丰
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SunFeng(ByVal mCode As String) As String
        Dim i As Integer
        Dim la, SANG As Integer
        Dim XJ As Integer
        Dim Tmt As String
        Dim n As Integer
        Dim YS As Integer
        Dim SumJ, m As Integer
        la = Len(mCode)
        If la <> 11 Then
            Throw New Exception("顺丰流水号长度必须为11位，验证号码长度为：" & mCode.Length & "位，不能使用此校验方式！")
            Return ""
        End If
        n = -1
        For i = 4 To la
            Tmt = Mid(mCode, i, 1)
            n = n + 1
            XJ = (19 - i - n) * Val(Tmt)
            YS = XJ Mod 10 : SANG = XJ \ 10
            SumJ = SumJ + YS + SANG
        Next i
        m = 10 - Val(Right(Str(SumJ), 1))
        Return m Mod 10
    End Function
    ''' <summary>
    ''' Only_Vector
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Only_Vector(ByVal mCode As String) As String
        Dim StrLen As Integer
        Dim TmpR As Integer
        Dim CanShu() As Integer
        StrLen = mCode.Length
        If CDbl(StrLen) <> 8 Then
            Throw New Exception("号码长度错误，长度必须为8位，请检查设置!")
        End If
        CanShu = New Integer() {8, 4, 5, 7, 9, 2, 1}
        For i = 0 To StrLen - 1
            TmpR = TmpR + Val(mCode.Substring(i, 1)) * CanShu(i)
        Next i
        Only_Vector = Right(Str(TmpR), 1)
    End Function
    ''' <summary>
    ''' 港中能达
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GangZhong(ByVal mCode As String) As String
        Dim StrLen As Single
        Dim Tmp1, Tmp2 As String
        StrLen = Len(mCode)
        If StrLen <> 10 Then
            Throw New Exception("流水号长度必须等于10位!")
        End If
        Tmp1 = Mid(mCode, 6, 1)
        Tmp2 = Mid(mCode, 10, 1)
        Return ((Val(Tmp1) + Val(Tmp2)) Mod 10)
    End Function
    ''' <summary>
    ''' 模11
    ''' </summary>
    ''' <param name="a"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ModEleven(ByVal a As String) As String
        Dim C, sum As Integer
        If Len(a) <> 8 Then
            Throw New Exception("号码长度必须为8位！")
        End If
        sum = CInt(Strings.Left(a, 1)) * 8 ' CInt(Left(a, 1)) * 8
        sum = sum + CInt(Mid(a, 2, 1)) * 6
        sum = sum + CInt(Mid(a, 3, 1)) * 4
        sum = sum + CInt(Mid(a, 4, 1)) * 2
        sum = sum + CInt(Mid(a, 5, 1)) * 3
        sum = sum + CInt(Mid(a, 6, 1)) * 5
        sum = sum + CInt(Mid(a, 7, 1)) * 9
        sum = sum + CInt(Mid(a, 8, 1)) * 7
        C = sum Mod 11
        If C = 0 Then
            Return 5
        End If
        If C = 1 Then
            Return 0
        End If
        Return 11 - C
    End Function
    ''' <summary>
    ''' 全一
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function QuanYI(ByVal mCode As String) As String
        Dim bar4 As Object
        Dim CL As Single
        Dim STR2, STR1, Str3 As String
        Dim Bar2, Bar1, Bar3 As String
        Dim Chk1, Chk2 As Integer
        Dim No2, Chk3, No1 As Integer
        CL = Len(mCode)
        If CL <> 11 Then
            Throw New Exception("流水号长度必须为11为，请检查输入的号码！")
            Return ""
        End If
        STR1 = Mid(mCode, 1, 4)
        STR2 = Mid(mCode, 5, 4)
        Str3 = Mid(mCode, 9, 3)
        Bar1 = Left(mCode, 9)
        Bar2 = Mid(mCode, 10, 1)
        Bar3 = Mid(mCode, 11, 1)
        Chk1 = Val(Bar1) Mod 7
        No1 = CInt(Str(Chk1) & Bar2)
        Chk2 = Val(CStr(No1)) Mod 7
        No2 = CInt(Str(Chk2) & Bar3)
        Chk3 = Val(CStr(No2)) Mod 7
        bar4 = Str(Chk3)
        QuanYI = Trim(bar4)
    End Function
    ''' <summary>
    ''' 模7
    ''' </summary>
    ''' <param name="mCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Mod7(ByVal mCode As String) As String
        Dim i As Integer
        Dim ca As Integer
        Dim TmpValue As Integer
        If Len(mCode) > 9 Then
            ca = Len(mCode) \ 8
            For i = 0 To ca
                TmpValue = Val(TmpValue & Mid(mCode, i * 8 + 1, 8)) Mod 7
            Next i
            Return CStr(TmpValue)
        Else
            Return CStr(CInt(mCode) Mod 7)
        End If
    End Function
    ''' <summary>
    ''' WaybillNoList
    ''' </summary>
    ''' <param name="Value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getWaybillNoList(ByVal Value As String) As String
        Dim i As Integer 'WaybillNoList
        Value = Trim(Value)
        If Len(Value) <> 11 Then Throw New Exception("号码长度必须为11位！") : Return ""
        Dim TmpValue1 As Integer
        Dim TmpValue2, TmpValue3 As Integer
        Dim Qsum As Long
        Dim m As Integer
        For i = 4 To 11
            TmpValue1 = Val(Mid(Value, i, 1))
            TmpValue3 = System.Math.Abs(CInt(Not (TmpValue1))) * TmpValue1
            TmpValue2 = TmpValue3 \ 10 + (TmpValue3 - 10 * (TmpValue3 \ 10))
            Qsum = Qsum + TmpValue2
        Next
        m = (Qsum \ 10 + 1) * 10
        Return CStr((m - Qsum) Mod 10)
    End Function
    ''' <summary>
    ''' 九福州
    ''' </summary>
    ''' <param name="Value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getAlemtat(ByVal Value As String) As String '???
        Value = Trim(Value)
        If Len(Value) <> 7 Then Throw New Exception("流水号加校验码长度为8位！") : Return ""
        Dim Str5, Str3, STR1, STR2, Str4, Str6 As String
        Dim Str7 As String
        Dim sum As Integer
        STR1 = Mid(Value, 1, 1)
        STR2 = Mid(Value, 2, 1)
        Str3 = Mid(Value, 3, 1)
        Str4 = Mid(Value, 4, 1)
        Str5 = Mid(Value, 5, 1)
        Str6 = Mid(Value, 6, 1)
        Str7 = Mid(Value, 7, 1)
        sum = Val(STR1) * 8 + Val(STR2) * 4 + Val(Str3) * 5 + Val(Str4) * 7 + Val(Str5) * 9 + Val(Str6) * 2 + Val(Str7) * 1
        getAlemtat = Right(CStr(sum), 1)
    End Function
    ''' <summary>
    ''' 港中能达无限制
    ''' </summary>
    ''' <param name="Value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getGangzhongNengDa(ByVal Value As String) As String '???????
        Dim a, b As String
        Dim sum As Short
        Dim Result As String
        Value = Trim(Value)
        a = Mid(Value, Len(Value) - 4, 1)
        b = Right(Value, 1)
        sum = Val(a) + Val(b)
        Result = Right(CStr(sum), 1)
        getGangzhongNengDa = Result
    End Function
    ''' <summary>
    ''' 中铁
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getCheckZhongtian(ByVal value As String) As String
        Dim SUM1 As Integer, Sum2 As Integer
        Dim sum As Integer, tmpSum As Integer
        Dim sIndex As Integer = 0
        For i = value.Length - 1 To 0 Step -1
            sIndex += 1
            If sIndex > 1 And sIndex Mod 2 = 0 Then
                SUM1 += CInt(value.Substring(i, 1))
            ElseIf sIndex > 2 And sIndex Mod 2 <> 0 Then
                Sum2 += CInt(value.Substring(i, 1))
            End If
        Next
        SUM1 = SUM1 * 3 + 18
        Sum2 = Sum2 * 2 - 3
        sum = (SUM1 + Sum2) * 7
        tmpSum = sum
        Do Until (tmpSum >= sum And tmpSum Mod 13 = 0)
            tmpSum += 1
        Loop
        Return Strings.Right((tmpSum - sum).ToString, 1)
    End Function
    ''' <summary>
    ''' 晋越
    ''' </summary>
    ''' <param name="a"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetJinYue(ByVal a As String) As String
        Dim i As Integer
        Dim Sum, Result As Integer
        Dim tmp As Object
        If Len(a) <> 11 Then Throw New Exception("流水号长度必须为11位！")
        For i = 1 To 10
            tmp = Mid(a, i, 1)
            Sum = Sum + Convert.ToInt32(tmp)
        Next
        Sum = Sum - Convert.ToInt32(Mid(a, 11, 1))
        Result = Sum Mod 9
        GetJinYue = Result
    End Function
    ''' <summary>
    ''' 黄马甲
    ''' </summary>
    ''' <param name="a"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function Gethuangmajia(ByVal a As String) As String  '???
        If Len(a) <> 10 Then Throw New Exception("流水号长度必须为10位！")
        Dim str9, str7, str5, str3, str1, str2, str4, str6, str8, str10 As String
        Dim sum2, sum1 As Integer
        Dim result As String
        str1 = Mid(a, 1, 1)
        str2 = Mid(a, 2, 1)
        str3 = Mid(a, 3, 1)
        str4 = Mid(a, 4, 1)
        str5 = Mid(a, 5, 1)
        str6 = Mid(a, 6, 1)
        str7 = Mid(a, 7, 1)
        str8 = Mid(a, 8, 1)
        str9 = Mid(a, 9, 1)
        str10 = Mid(a, 10, 1)
        sum1 = CInt(str1) + CInt(str2) + CInt(str3) + CInt(str4) + CInt(str5)
        sum2 = CInt(str6) + CInt(str7) + CInt(str8) + CInt(str9) + CInt(str10)
        sum1 = sum1 Mod 7
        sum2 = sum2 Mod 7
        result = sum1 & sum2
        Return result
    End Function
    ''' <summary>
    ''' 深圳石化
    ''' </summary>
    ''' <param name="Value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getSHCheck(ByVal Value As String) As String
        Dim sum As Integer = 0, count As Integer
        Dim tmp As String, Result As String
        For i As Integer = 0 To Value.Length - 1
            tmp = Value.Substring(i, 1)
            sum = sum + Asc(Convert.ToChar(tmp))
        Next
        count = sum Mod 43
        Result = String.Format("{0:D2}", count)
        Return Result
    End Function
    ''' <summary>
    ''' 城市应急
    ''' </summary>
    ''' <param name="Value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CityExpress(ByVal Value As String) As String
        Const tmpValue As String = "243674494654"
        If Value.Length <> 12 Then Throw New Exception("号码长度必须为12位！")
        Dim sum As Integer
        For i = 0 To Value.Length - 1
            sum += Val(Value.Substring(i, 1)) * Val(tmpValue.Substring(i, 1))
        Next
        Return Strings.Right(sum.ToString, 1)
    End Function
    ''' <summary>
    ''' 三凌空调
    ''' </summary>
    ''' <param name="Value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getSanlinkongtiao(ByVal Value As String) As String
        Dim sum As Integer = 0
        For i = 0 To Value.Length - 1
            sum += AscW(Value.Substring(i, 1))
        Next
        Return (sum Mod 10).ToString
    End Function
    ''' <summary>
    ''' 模10
    ''' </summary>
    ''' <param name="a"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getMod10(ByVal a As String) As String
        Dim Sum1 As Integer = 0, Sum2 As Integer = 0
        Dim Result As Integer, La As Integer
        La = Len(a)
        For i = 1 To La Step 1
            If i Mod 2 = 0 Then
                Sum2 = Sum2 + CInt(Mid(a, i, 1))
            Else
                Sum1 = Sum1 + CInt(Mid(a, i, 1))
            End If
        Next
        Result = (Sum1 * 3 + Sum2) Mod 10
        If Result = 0 Then
            Return 0
        Else
            Return 10 - Result
        End If
    End Function
End Class
